package org.opu.db_vdumper.ui.component.sql;

import java.awt.Color;
import java.awt.event.MouseEvent;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableModel;

/**
 *
 * @author yura
 */
public class TabelWithToolType extends JTable {

    private Object head[] ;

    public TabelWithToolType(Object[][] rowData, Object[] columnNames) {
        super(rowData, columnNames);
        this.head = columnNames;

        setDragEnabled(false);
        setBackground(Color.WHITE);
        getTableHeader().setReorderingAllowed(false);
        setRowSelectionAllowed(true);
        setFillsViewportHeight(true);
    }

    @Override
    public String getToolTipText(MouseEvent e) {
        int c = columnAtPoint(e.getPoint());
        int r = rowAtPoint(e.getPoint());

        if (c < 0 || r < 0) {
            return getToolTipText();
        }

        try {
            Object valueAt = getValueAt(r, c);

            if (valueAt == null) {
                return null;
            } else {
                return String.valueOf(valueAt);
            }
        } catch (Exception ex) {
            return getToolTipText();
        }
    }

    @Override
    protected JTableHeader createDefaultTableHeader() {
        return new JTableHeader(columnModel) {

            @Override
            public String getToolTipText(MouseEvent e) {
                int index = columnModel.getColumnIndexAtX(e.getPoint().x);
                int rIndex = columnModel.getColumn(index).getModelIndex();

                if (head.length > rIndex) {
                    Object hv = columnModel.getColumn(rIndex).getHeaderValue();

                    if (hv != null) {
                        return String.valueOf(hv);
                    } else {
                        return getToolTipText();
                    }
                } else {
                    return getToolTipText();
                }
            }
        };
    }
}
